import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const key = 'user';
const store = new Vuex.Store({
    //定义状态
    state() {
        return {
            user: null
        }
    },
    getters: {
        getStorage (state) {
            if(!state.user) {
                //user:{token: value, key1: value1, key2: value2}
                state.user = JSON.parse(localStorage.getItem(key));
            }
            return state.user;
        }
    },
    mutations: {
        $_setStorage(state, value) {
            state.user = value;
            localStorage.setItem(key, JSON.stringify(value));
        },
        $_removeStorage(state) {
            state.user = null;
            localStorage.removeItem(key);
        }

    },
    actions: {

    }
});

export default store;
